﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Cms.Domain;
using Cms.Domain.Comparers;
using iLib.Web.UI;
using iLib.Web.UI.WebControls;
using Image = System.Drawing.Image;
using SortDirection = Cms.Domain.SortDirection;

namespace Cms.Web.UI.Admin.HelperClasses
{
    public class MediaHelper
    {
        public static void rpFiles_ItemDataBound(object sender, RepeaterItemEventArgs e, string pixelsTranslation,
                                                 string allowAccessForEveryoneTranslation)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var file = (MediaFile)e.Item.DataItem;
            var chkMedia = (iCheckBox)e.Item.FindControl("chkMedia");
            var litID = (Literal)e.Item.FindControl("litID");
            var litFile = (Literal)e.Item.FindControl("litFile");
            var imgFile = (HtmlImage)e.Item.FindControl("imgFile");
            var litSize = (Literal)e.Item.FindControl("litSize");
            var litInfo = (Literal)e.Item.FindControl("litInfo");
            var litUploadedTimestamp = (Literal)e.Item.FindControl("litUploadedTimestamp");
            var litSortKey = (Literal)e.Item.FindControl("litSortKey");
            var litCategories = (Literal)e.Item.FindControl("litCategories");
            var litUserGroups = (Literal)e.Item.FindControl("litUserGroups");
            var litThumbnail = (Literal)e.Item.FindControl("litThumbnail");
            var litDescription = (Literal)e.Item.FindControl("litDescription");
            chkMedia.Value = file.ID.ToString();
            litID.Text = "<span id=\"M" + file.ID + "\">";
            litFile.Text = file.Name;
            imgFile.Src = "Admin/Img/icons/page_white.png";
            litSize.Text = Math.Round((double)file.Size / 1024, 0) + " KB";
            switch (file.Extension.ToLower())
            {
                case "gif":
                case "jpg":
                case "png":
                    imgFile.Src = "Admin/Img/icons/picture.png";
                    if (File.Exists(Url.PhysicalPath + file.GetPhysicalPath()))
                    {
                        litThumbnail.Text = "<br /><img src=\"" + Url.VirtualPath + "Thumbnail.aspx?file=" +
                                            HttpContext.Current.Server.UrlEncode(file.GetVirtualPath()) +
                                            "&width=100&height=50\" alt=\"" +
                                            file.Name + "\" class=\"thumbnail\" />";
                        litThumbnail.Visible = true;
                        Image bmp = Image.FromFile(Url.PhysicalPath + file.GetPhysicalPath());
                        litInfo.Text = bmp.Width + " x " + bmp.Height + " " + pixelsTranslation;
                        bmp.Dispose();
                    }
                    break;
                case "zip":
                    imgFile.Src = "Admin/Img/icons/package.png";
                    break;
                case "js":
                    imgFile.Src = "Admin/Img/icons/script.png";
                    break;
                case "pdf":
                    imgFile.Src = "Admin/Img/icons/page_white_acrobat.png";
                    break;
                case "xls":
                case "xlsx":
                    imgFile.Src = "Admin/Img/icons/page_white_excel.png";
                    break;
                case "doc":
                case "docx":
                    imgFile.Src = "Admin/Img/icons/page_white_word.png";
                    break;
                case "ppt":
                case "pptx":
                    imgFile.Src = "Admin/Img/icons/page_white_powerpoint.png";
                    break;
                case "txt":
                    imgFile.Src = "Admin/Img/icons/page_white_text.png";
                    break;
                case "mdb":
                    imgFile.Src = "Admin/Img/icons/database.png";
                    break;
                case "php":
                    imgFile.Src = "Admin/Img/icons/page_white_php.png";
                    break;
                case "asp":
                    imgFile.Src = "Admin/Img/icons/page_white_visualstudio.png";
                    break;
                case "swf":
                    imgFile.Src = "Admin/Img/icons/page_white_flash.png";
                    break;
                case "htm":
                case "html":
                case "xml":
                    imgFile.Src = "Admin/Img/icons/page_white_code.png";
                    break;
                case "mp3":
                    imgFile.Src = "Admin/Img/icons/sound.png";
                    break;
            }
            litUploadedTimestamp.Text = file.UploadedTimestamp.ToShortDateString();
            litSortKey.Text = file.SortKey;
            string categories = string.Empty;
            foreach (Category mc in file.GetAllCategories())
            {
                if (file.Folder != null && mc.ContainsMediaFolder(file.Folder))
                    categories += "<span class=\"inheritedCategory\">" + mc.Name + "</span>";
                else
                    categories += mc.Name;
                categories += ", ";
            }
            categories = categories.TrimEnd(", ".ToCharArray());
            litCategories.Text = categories;
            string requiredUserGroups = string.Empty;
            var allRequiredUserGroups = new List<UserGroup>(file.GetRequiredUserGroups());
            allRequiredUserGroups.Sort(new UserGroupNameComparer(SortDirection.Ascending));
            foreach (UserGroup group in allRequiredUserGroups)
            {
                if (file.Folder != null && group.AllowsMediaFolder(file.Folder))
                    requiredUserGroups += "<span class=\"inheritedCategory\">" + group.Name + "</span>";
                else
                    requiredUserGroups += group.Name;
                requiredUserGroups += ", ";
            }
            requiredUserGroups = requiredUserGroups.TrimEnd(", ".ToCharArray());
            if (requiredUserGroups == string.Empty)
                requiredUserGroups = allowAccessForEveryoneTranslation;
            litUserGroups.Text = requiredUserGroups;
            int localeID = file.Site.DefaultLocale.ID;
            if (litDescription == null || !file.LocalizedProperties.ContainsKey(localeID))
                return;
            litDescription.Text = file.LocalizedProperties[localeID].Title + Environment.NewLine +
                                  file.LocalizedProperties[localeID].Description;
        }
    }
}